﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace VisionarySystems.Library
{
    public class RegistryUtils
    {
        public static void WriteRegistryValue(string SubKey, string KeyName, string Value)
        {
            using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(SubKey))
            {
                key.SetValue(KeyName, Value);
            }
            //key.Close();
        }

        public static string ReadRegistryValue(string SubKey, string KeyName, string DefaultValue)
        {
            using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(SubKey))
            {
                //return key.GetValue (KeyName);

                string[] valueNames = key.GetValueNames();
                foreach (string valueName in valueNames)
                {
                    if (valueName == KeyName)
                    {
                        RegistryValueKind rvk = key.GetValueKind(valueName);
                        switch (rvk)
                        {
                            case RegistryValueKind.String:
                                return key.GetValue(valueName).ToString();
                            default:
                                throw new Exception("The key is not a string");
                        }
                    }
                }
                return DefaultValue; // Key not found
            }
        }
    }
}
